
#include "mMatrix3x3.h"

mMatrix3x3::mMatrix3x3(void)
{
	Identity();
}

mMatrix3x3::mMatrix3x3(const mMatrix3x3 &a)
{
	m00 = a.m00;
	m01 = a.m01;
	m02 = a.m02;
	m10 = a.m10;
	m11 = a.m11;
	m12 = a.m12;
	m20 = a.m20;
	m21 = a.m21;
	m22 = a.m22;
}

mMatrix3x3::mMatrix3x3(float nm00, float nm01, float nm02,
						 float nm10, float nm11, float nm12,
						 float nm20, float nm21, float nm22)
{
	m00 = nm00;
	m01 = nm01;
	m02 = nm02;
	m10 = nm10;
	m11 = nm11;
	m12 = nm12;
	m20 = nm20;
	m21 = nm21;
	m22 = nm22;
}

mMatrix3x3::mMatrix3x3(float m[9])
{
	m00 = m[0];
	m01 = m[1];
	m02 = m[2];
	m10 = m[3];
	m11 = m[4];
	m12 = m[5];
	m20 = m[6];
	m21 = m[7];
	m22 = m[8];
}

mMatrix3x3::mMatrix3x3(float m[3][3])
{
	m00 = m[0][0];
	m01 = m[0][1];
	m02 = m[0][2];
	m10 = m[1][0];
	m11 = m[1][1];
	m12 = m[1][2];
	m20 = m[2][0];
	m21 = m[2][1];
	m22 = m[2][2];
}

mMatrix3x3::~mMatrix3x3(void)
{
}

mMatrix3x3 &mMatrix3x3::operator =(const mMatrix3x3 &a)
{
	m00 = a.m00;
	m01 = a.m01;
	m02 = a.m02;
	m10 = a.m10;
	m11 = a.m11;
	m12 = a.m12;
	m20 = a.m20;
	m21 = a.m21;
	m22 = a.m22;
	return *this;
}

bool mMatrix3x3::operator ==(const mMatrix3x3 &a) const
{
	return m00 == a.m00 && m01 == a.m01 && m02 == a.m02
		&& m10 == a.m10 && m11 == a.m11 && m12 == a.m12
		&& m22 == a.m22 && m21 == a.m21 && m22 == a.m22;
}

bool mMatrix3x3::operator !=(const mMatrix3x3 &a) const
{
	return m00 != a.m00 || m01 != a.m01 || m02 != a.m02
		|| m10 != a.m10 || m11 != a.m11 || m12 != a.m12
		|| m22 != a.m22 || m21 != a.m21 || m22 != a.m22;
}

mMatrix3x3 mMatrix3x3::operator -(void) const
{
	mMatrix3x3 m(*this);
	m.m00 *= -1;
	m.m01 *= -1;
	m.m02 *= -1;
	m.m10 *= -1;
	m.m11 *= -1;
	m.m21 *= -1;
	m.m20 *= -1;
	m.m21 *= -1;
	m.m22 *= -1;
	return m; 
}

mMatrix3x3 mMatrix3x3::operator +(const mMatrix3x3 &a) const
{
	mMatrix3x3 m(*this);
	m.m00 += a.m00;
	m.m01 += a.m01;
	m.m02 += a.m02;
	m.m10 += a.m10;
	m.m11 += a.m11;
	m.m12 += a.m12;
	m.m20 += a.m20;
	m.m21 += a.m21;
	m.m22 += a.m22;
	return m;
}

mMatrix3x3 mMatrix3x3::operator -(const mMatrix3x3 &a) const
{
	mMatrix3x3 m(*this);
	m.m00 -= a.m00;
	m.m01 -= a.m01;
	m.m02 -= a.m02;
	m.m10 -= a.m10;
	m.m11 -= a.m11;
	m.m12 -= a.m12;
	m.m20 -= a.m20;
	m.m21 -= a.m21;
	m.m22 -= a.m22;
	return m;
}

mMatrix3x3 mMatrix3x3::operator *(const mMatrix3x3 &a) const
{
	mMatrix3x3 m;
	m.m00 = (m00 * a.m00) + (m01 * a.m10) + (m02 * a.m20);
	m.m01 = (m00 * a.m01) + (m01 * a.m11) + (m02 * a.m21);
	m.m02 = (m00 * a.m02) + (m01 * a.m12) + (m02 * a.m22);
	m.m10 = (m10 * a.m00) + (m11 * a.m10) + (m12 * a.m20);
	m.m11 = (m10 * a.m01) + (m11 * a.m11) + (m12 * a.m21);
	m.m12 = (m10 * a.m02) + (m11 * a.m12) + (m12 * a.m22);
	m.m20 = (m20 * a.m00) + (m21 * a.m10) + (m22 * a.m20);
	m.m21 = (m20 * a.m01) + (m21 * a.m11) + (m22 * a.m21);
	m.m22 = (m20 * a.m02) + (m21 * a.m12) + (m22 * a.m22);
	return m;
}

mVector3 mMatrix3x3::operator *(const mVector3 &a) const
{
	mVector3 v;
	v.X = (m00 * a.X) + (m01 * a.Y) + (m02 * a.Z);
	v.Y = (m10 * a.X) + (m11 * a.Y) + (m12 * a.Z);
	v.Z = (m20 * a.X) + (m21 * a.Y) + (m22 * a.Z);
	return v;
}

mMatrix3x3 mMatrix3x3::operator *(float a) const
{
	mMatrix3x3 m(*this);
	m.m00 *= a;
	m.m01 *= a;
	m.m02 *= a;
	m.m10 *= a;
	m.m11 *= a;
	m.m21 *= a;
	m.m20 *= a;
	m.m21 *= a;
	m.m22 *= a;
	return m;	
}

mMatrix3x3 &mMatrix3x3::operator +=(const mMatrix3x3 &a)
{
	m00 += a.m00;
	m01 += a.m01;
	m02 += a.m02;
	m10 += a.m10;
	m11 += a.m11;
	m12 += a.m12;
	m20 += a.m20;
	m21 += a.m21;
	m22 += a.m22;
	return *this;
}

mMatrix3x3 &mMatrix3x3::operator -=(const mMatrix3x3 &a)
{
	m00 -= a.m00;
	m01 -= a.m01;
	m02 -= a.m02;
	m10 -= a.m10;
	m11 -= a.m11;
	m12 -= a.m12;
	m20 -= a.m20;
	m21 -= a.m21;
	m22 -= a.m22;
	return *this;
}

mMatrix3x3 &mMatrix3x3::operator *=(float a)
{
	m00 *= a;
	m01 *= a;
	m02 *= a;
	m10 *= a;
	m11 *= a;
	m21 *= a;
	m20 *= a;
	m21 *= a;
	m22 *= a;
	return *this;	
}

float &mMatrix3x3::operator [](int index)
{
	switch (index)
	{
	case 0:
		return m00;
	case 1:
		return m01;
	case 2:
		return m02;
	case 3:
		return m10;
	case 4:
		return m11;
	case 5:
		return m12;
	case 6:
		return m20;
	case 7:
		return m21;
	case 8:
		return m22;
	default:
		return m00;
	}
}

void mMatrix3x3::Identity(void)
{
	m00 = m11 = m22 = 1.0f;
	m01 = m02 = m10 = m12 = m20 = m21 = 0.0f;
}

void mMatrix3x3::createXRotation(float angle)
{
	m00 = 1.0f;
	m01 = m02 = m10 = m20 = 0.0f;
	m11 = m22 = cos(angle);
	m12 = m21 = sin(angle);
	m21 *= -1;
}

void mMatrix3x3::createYRotation(float angle)
{
	m11 = 1.0f;
	m01 = m10 = m12 = m21 = 0.0f;
	m00 = m22 = cos(angle);
	m02 = m20 = sin(angle);
	m02 *= -1;
}

void mMatrix3x3::createZRotation(float angle)
{
	m22 = 1.0f;
	m02 = m12 = m20 = m21 = 0.0f;
	m00 = m11 = cos(angle);
	m01 = m10 = sin(angle);
	m10 *= -1;
}

//void mMatrix3x3::createTranslation(mVector3 translation)
//{
//}

void mMatrix3x3::createScale(float scale)
{
	m00 = m11 = m22 = scale;
	m01 = m02 = m10 = m12 = m20 = m21 = 0.0f;
}